package wkt.image;

import cn.hutool.core.io.FileUtil;
import com.pig4cloud.pigx.common.oss.OssProperties;
import com.pig4cloud.pigx.common.oss.service.OssTemplate;
import net.xnzn.framework.id.Id;
import net.xnzn.framework.secure.filter.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/image")
@Validated
@RequiresAuthentication
public class ImageController {

    @Autowired
    OssTemplate ossTemplate;
    @Autowired
    OssProperties ossProperties;

    @PostMapping("/upload")
    public String getExamDate(MultipartFile file) throws Exception {
        final String objectName = "question/" + Id.nextString() + "." + FileUtil.extName(file.getOriginalFilename());
        ossTemplate.putObject(ossProperties.getBucketName(), objectName, file.getInputStream());
        return "https://" + ossProperties.getBucketName() + "." + ossProperties.getEndpoint() + "/" + objectName;
    }
}
